using System;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;
using Refract.Views;

namespace Refract.Navigation
{
    /// <summary>
    /// Describes a page for the navigation subsystem
    /// </summary>
    public class PageItem
    {
        #region Fields

        private bool _Active;
        private DelegateCommand<string> _Command;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether the page is active.
        /// </summary>
        /// <value><c>true</c> if active; otherwise, <c>false</c>.</value>
        public bool Active
        {
            get
            {
                return _Active;
            }
            set
            {
                _Active = value;
                if (_Command != null)
                {
                    _Command.RaiseCanExecuteChanged();
                }
            }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public String Name { get; private set; }

        
        /// <summary>
        /// Gets or sets the command.
        /// </summary>
        /// <value>The command.</value>
        public ICommand Command
        {
            get
            {
                return _Command;
            }
            private set
            {
                _Command = (DelegateCommand<string>)value;
            }
        }

        /// <summary>
        /// Gets or sets the type of the view model.
        /// </summary>
        /// <value>The type of the view model.</value>
        public Type ViewModelType { get; private set; } 

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PageItem"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="viewModelType">Type of the view model.</param>
        /// <param name="targetCommand">The target command.</param>
        public PageItem(string name, Type viewModelType)
        {
            Active = true;
            Name = name;

            if (!viewModelType.IsSubclassOf(typeof(ViewModel)))
            {
                throw new ArgumentException("Supplied type is not a ViewModel.", "ViewModel");
            }
            ViewModelType = viewModelType;

            Command = new DelegateCommand<string>(Navigate, p => { return Active; });
            
        }

        private void Navigate(string page)
        {
            if (OnNavigate != null)
            {
                OnNavigate(this, new EventArgs<string>(page));
            }
        }

        public event EventHandler<EventArgs<string>> OnNavigate;

        #endregion
    }
}
